package net.gyapp.markim.module.message.adapter;

import android.text.TextUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;

import net.gyapp.markim.R;
import net.gyapp.markim.core.ImHelper;
import net.gyapp.markim.core.entity.ConversationEntity;
import net.gyapp.markim.module.basic.base.BaseRecyclerHolder;

import org.jim.common.packets.ChatType;
import org.jim.common.packets.MsgType;

/**
 * Author:  winds
 * Data:    2018/7/7
 * Version: 1.0
 * Desc:
 */


public class MessageAdapter extends BaseQuickAdapter<ConversationEntity, BaseRecyclerHolder> {
    public MessageAdapter() {
        super(R.layout.item_message);
    }

    /**
     * 获取未读消息数目
     *
     * @return
     */
    public int getUnReadCount() {
        int count = 0;
        if (mData != null) {
            for (int i = 0; i < mData.size(); i++) {
                count += mData.get(i).unReadNum;
            }
        }
        return count;
    }

    @Override
    protected void convert(BaseRecyclerHolder helper, ConversationEntity item) {
        helper.setImageManager(mContext, R.id.iv_header, item.avatar, item.chatType == ChatType.CHAT_TYPE_PUBLIC.getNumber() ? R.drawable.placeholder_group_header : R.drawable.placeholder_header);
        helper.setText(R.id.tv_name, TextUtils.isEmpty(item.name) ? item.sessionIdStr : item.name);
        if (item.unReadNum > 0) {
            helper.setVisibleText(R.id.msg, String.valueOf(item.unReadNum > 99 ? 99 : item.unReadNum));
        } else {
            helper.setVisible(R.id.msg, false);
        }

        switch (MsgType.forNumber(item.latestMsgType)) {
            case MSG_TYPE_TEXT:
                if (item.chatType == ChatType.CHAT_TYPE_PUBLIC.getNumber()) {
                    helper.setText(R.id.tv_message, String.format("%1$s%2$s", TextUtils.isEmpty(item.latestNick) ? "" : item.latestNick + ":", item.latestMsg));
                } else {
                    helper.setText(R.id.tv_message, item.latestMsg);
                }
                break;
            case MSG_TYPE_IMG:
                if (item.chatType == ChatType.CHAT_TYPE_PUBLIC.getNumber()) {
                    helper.setText(R.id.tv_message, String.format("%1$s%2$s", TextUtils.isEmpty(item.latestNick) ? "" : item.latestNick + ":", "[图片]"));
                } else {
                    helper.setText(R.id.tv_message, "[图片]");
                }
                break;
            case MSG_TYPE_VIDEO:
                if (item.chatType == ChatType.CHAT_TYPE_PUBLIC.getNumber()) {
                    helper.setText(R.id.tv_message, String.format("%1$s%2$s", TextUtils.isEmpty(item.latestNick) ? "" : item.latestNick + ":", "[视频]"));
                } else {
                    helper.setText(R.id.tv_message, "[视频]");
                }
                break;
            case MSG_TYPE_VOICE:
                if (item.chatType == ChatType.CHAT_TYPE_PUBLIC.getNumber()) {
                    helper.setText(R.id.tv_message, String.format("%1$s%2$s", TextUtils.isEmpty(item.latestNick) ? "" : item.latestNick + ":", "[语音]"));
                } else {
                    helper.setText(R.id.tv_message, "[语音]");
                }
                break;
        }

        helper.setText(R.id.tv_time, ImHelper.formatTime(item.latestTime != null ? item.latestTime : System.currentTimeMillis()));
    }
}
